<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/1/23
 * Time: 上午10:34
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


class GoodsLevel extends Base
{
    protected $table = "goods_level";
    protected $fields = ['id', 'level', 'name', 'icon'];

    protected $CacheGoodsLevelListKey = 'CACHE:GOODS:LEVEL:LIST:MD5:%s';


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function info(array $where)
    {
        return DB::table($this->table)
            ->where($where)
            ->first();
    }


    public function list(array $where)
    {
        $mdStr = md5( json_encode($where) );
        $CacheKey = $this -> CacheGoodsLevelListKey;
        $CacheKey = sprintf($CacheKey, $mdStr);
        $Cache = Redis::get($CacheKey);
        $list = json_decode($Cache, true);
        if (empty($list)){
            $res = DB::table($this->table)
                ->where($where)
                ->orderBy('level', 'asc')
                ->get();
            $list = self::objToArray($res);
            if ($list){
                Redis::set($CacheKey, json_encode($list), 'EX', 7200);
            }

        }

        return $list;

    }



}